package com.java2104.controller;

import com.java2104.entity.Dept;
import com.java2104.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestBody List<Integer> ids){
        Integer sum=0;
        for (int i=0;i<ids.size();i++){
            Integer count =deptService.findKey(ids.get(i));
            if (count==0){
                int c=deptService.delete(ids.get(i));
                sum++;
            }
        }
        return sum+"dept(s)success,"+(ids.size()-sum)+"dept(s) failed";
}

    @RequestMapping("/list")
    @ResponseBody
    public List<Dept> list(String name){
        return deptService.selectDeptName(name);
    }

    @RequestMapping("/showUpdateDept")
    public void showUpdateDept(Model model ,Integer id){
        model.addAttribute(deptService.get(id));
    }

    @PostMapping("/update")
    @ResponseBody
    public Map update(Dept dept){
        return deptService.updateremark(dept);
    }
    @PostMapping("/add")
    @ResponseBody
    public Map add(Dept dept){
        return deptService.save(dept);
    }

}
